package com.jack.helloworld.activity;

import android.app.ActivityManager;
import android.content.Context;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.jack.helloworld.R;

public class SystemServiceActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_system_service);
    }

    public void doClick(View view) {
        switch (view.getId()) {
            case R.id.btnWifiStatus:
                ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
                NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
                if (networkInfo != null && networkInfo.isConnected()) {
                    Toast.makeText(this, R.string.network_connected, Toast.LENGTH_SHORT).show();
                    if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                        Toast.makeText(this, R.string.wifi_connected, Toast.LENGTH_SHORT).show();
                    } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                        Toast.makeText(this, R.string.mobile_data_connected, Toast.LENGTH_SHORT).show();
                    }
                } else {
                    Toast.makeText(this, R.string.network_not_connected, Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.btnOpenOrCloseWifi:
                WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
                if (wifiManager != null) {
                    if (wifiManager.isWifiEnabled()) {
                        Toast.makeText(this, R.string.wifi_is_closed, Toast.LENGTH_SHORT).show();
                        wifiManager.setWifiEnabled(false);
                    } else {
                        Toast.makeText(this, R.string.wifi_is_open, Toast.LENGTH_SHORT).show();
                        wifiManager.setWifiEnabled(true);
                    }
                }

                break;
            case R.id.btnVolumeStatus:
                AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
                if (audioManager != null) {
                    int streamMaxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_SYSTEM);
                    int streamVolume = audioManager.getStreamVolume(AudioManager.STREAM_RING);
                    Toast.makeText(this, "最大音量值：" + streamMaxVolume, Toast.LENGTH_SHORT).show();
                    Toast.makeText(this, "当前音量值：" + streamVolume, Toast.LENGTH_SHORT).show();
                }

                break;
            case R.id.btnTopPkName:
                Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
                ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
                if (activityManager != null) {
                    String packageName = activityManager.getRunningTasks(1).get(0).topActivity.getPackageName();
                    Toast.makeText(this, "当前应用包名：" + packageName, Toast.LENGTH_SHORT).show();
                }

                break;
        }
    }
}
